/* $Revision: $ $Date: $
 * 
 * Copyright (C) 2009 Robert Stephan
 * 
 * This file is part of *** CaCaO - Card Catalogs Online ***
 *
 * CaCaO is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * CaCao is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package cacao.server;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Properties;

import cacao.client.CaCaOMessages;

public class CaCaOServer {
	Properties props = null;
	CaCaOMessages messages = null;
	private static CaCaOServer _instance;

	private CaCaOServer(){
		props = new Properties();
		try {
			//Properties.load(Reader r) is code from java 6
			props.load(this.getClass().getClassLoader().getResourceAsStream("cacao.properties"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static CaCaOServer getInstance(){
		if(_instance == null){
			_instance = new CaCaOServer();
		}
		return _instance;
	}

	public Properties getProperties(){
		return props;
	}

	public Connection getDBConnection(){
		Connection conn=null;
		try {
			Class.forName(props.getProperty("CaCaO.server.db.classname")).newInstance();
			String url = props.getProperty("CaCaO.server.db.connectionURL");
			conn = DriverManager.getConnection(url);
			String schema = props.getProperty("CaCaO.server.db.schema").trim();
			if(schema!=null && !schema.equals("")){
				if(url.contains("jdbc:postgresql:")){
					Statement stmt = conn.createStatement();
					stmt.execute("SET search_path="+schema+";");
					stmt.close();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;	
	}
}
